home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 3: The Continuation / 17-Bit_The_Continuation_Disc.iso / amigan / amigan 20 / jimutils / dfree.a < prev    next >
Text File  |  1994-01-27  |  8KB  |  307 lines

  1. * DFree.a:    An assembler disk free space lister.  I came up with this
  2. *        as a protest against INFO.  I don't CARE how many blocks
  3. *        are free, I want to know how much space I've got in BYTES,
  4. *        considering everything else tells me file sizes in bytes,
  5. *        etc., etc.
  6. *
  7. *        Working version finished 12/14/86 jec
  8. *        Added support for volume name 12/16/86 jec
  9. *
  10. * Included equate files.
  11. * -----------------------------------------------------------------------
  12.     NOLIST
  13.     INCLUDE 'exec/types.i'
  14.     INCLUDE 'exec/libraries.i'
  15.     INCLUDE 'exec/memory.i'
  16.     INCLUDE 'libraries/dos.i'
  17.     INCLUDE 'libraries/dosextens.i'
  18.     INCLUDE    'offsets/exec.i'
  19.     INCLUDE    'offsets/dos.i'
  20.     LIST
  21.  
  22.     ifd    CAPE
  23.     EXEOBJ
  24.     objfile    'ram:DFree'
  25.     optimon    -1
  26.     endc
  27.  
  28. * Local Equates
  29. * ------------------------------------------------------------------------
  30. TRUE    EQU    -1
  31. FALSE    EQU    0
  32. RAM    EQU    'RAM'<<8
  33.  
  34. * Stack frame variables
  35. * ------------------------------------------------------------------------
  36. v_STRPTR    EQU    -128
  37. v_SIZE        EQU    -124
  38. v_STRING    EQU    -120
  39. v_OUTPUT    EQU    -88
  40.  
  41. * The code segment
  42. * -------------------------------------------------------------------------
  43.     link    a4,#-128
  44.     jsr    argtrim         ; clean up command line
  45.     MOVEM.L D0/A0-A2,-(SP)        ; Save command line (OpenLibrary trashes).
  46.     moveq    #0,d0
  47.  
  48.     ;------ get Exec's library base pointer:
  49.     LEA.L    DOSName(PC),A1        ; name of dos library
  50.     moveq    #33,d0            ; at LEAST AmigaDOS 1.2
  51.  
  52.     move.l    _AbsExecBase,a6
  53.     Call    OpenLibrary
  54.     MOVE.L    D0,A6            ; Save library pointer (always in A6).
  55.     BNE.S    gotdos
  56.  
  57.     ; Should really issue an alert here...
  58.     moveq    #RETURN_FAIL,D0     ; give up
  59.     bra    FINISHED
  60.  
  61. gotdos:
  62.  
  63.     ; REGISTER USAGE:
  64.     ;    A0    = scratch after command line used.
  65.     ;    A4    = stack frame
  66.     ;    A5    = InfoData address
  67.     ;    A6    = dos library base pointer
  68.     ;    D1-3    = AmigaDOS scratch argument registers
  69.     ;    D4    = current directory lock
  70.     ;    D5    = output handle
  71.     ;    D6    = root lock flag
  72.  
  73. *    Obtain the output handle (needed for write)
  74.     Call    Output
  75.     MOVE.L    D0,D5            ; Save for the write
  76.  
  77.     MOVEM.L (SP)+,D0/A0-A2        ; restore command line etc.
  78.  
  79. *    Setup D6 as the restore directory flag
  80.     moveq    #FALSE,D6
  81.  
  82. *    Now check if we have an argument. If we have, attempt to lock it.
  83. *    Otherwise, go for current directory.
  84.     cmpi.b    #0,(A0)         ; a null string for an argument ?
  85.     beq.s    CURDIR            ; yes
  86.  
  87.     ; Lock to specified directory. Place name in D1, access mode in D2
  88.     move.l    a0,d1
  89.     move.l    #ACCESS_READ,d2
  90.     Call    Lock
  91.     move.l    d0,d4            ; remember for later
  92.     beq    nolock            ; did we get a lock?
  93.     bra.s    uselock         ; for the Examine
  94.  
  95. CURDIR:
  96.     ; Get lock to current directory. The most bulletproof way of doing this
  97.     ; (due to bugs in the V1.1 RAM: handler) is to make the root directory
  98.     ; the current one, because CurrentDir will return the old lock value.
  99.     moveq    #0,D1
  100.     Call    CurrentDir        ; got the lock
  101.     move.l    d0,d4            ; for later
  102.     moveq    #TRUE,d6        ; remember to cd
  103.  
  104. uselock:
  105.     ; A lock in D1 and a InfoData address in D2 are all that are needed.
  106.     ; We allocmem the InfoData to assure alignment.
  107.  
  108.     move.l    #id_SIZEOF,D0        ; size of the block
  109.     move.l    #MEMF_PUBLIC,D1     ; memory requirement
  110.  
  111.     MOVEM.L D2-D7/A0-A6,-(SP)    ; in case AllocMem trashes (it does...)
  112.     move.l    _AbsExecBase,a6
  113.     Call    AllocMem        ; args D0, D1 returns into D0
  114.     MOVEM.L (SP)+,D2-D7/A0-A6    ; restore registers
  115.     move.l    d0,a5            ; save it for later
  116.     beq    nomem            ; but did it work?
  117.  
  118.     move.l    d0,d2            ; InfoData address
  119.     move.l    d4,d1            ; the lock
  120.  
  121.     Call    Info            ; see what the lock corresponds to.
  122.  
  123.     cmpi.l    #FALSE,d0        ; don't need to cd in cleanup code
  124.     beq    noinfo
  125.     move.l    d2,a0            ; infodata pointer into a0
  126.     move.l    id_NumBlocks(a0),d2    ; # of blocks on disk
  127.     sub.l    id_NumBlocksUsed(a0),d2 ; # already used
  128.     moveq    #0,d0
  129.     move.l    d0,d1            ; initialize multiplier
  130.     move.l    id_BytesPerBlock(a0),d1
  131.     subq.l    #1,d1            ; adjust for loop
  132. 1$:
  133.     add.l    d2,d0            ; add to total
  134.     dbra    d1,1$            ; loop 'til done
  135.     move.l    d0,v_SIZE(a4)        ; save for print
  136.     tst.l    id_VolumeNode(a0)    ; ...RAM: doesn't have a Volume
  137.     bne.s    df1            ; ...Node, so returns 0.
  138.     move.l    #RAM,v_STRING(a4)    ; store RAM name in output string
  139.     bra.s    df2
  140. df1:
  141.     move.l    id_VolumeNode(a0),d0    ; here goes a mess caused by
  142.     asl.l    #2,d0            ; ...using BPTRs to show where
  143.     move.l    d0,a0            ; ...things are.  We need all
  144.     move.l    40(a0),d0        ; ...of this mess just to find
  145.     asl.l    #2,d0            ; ...the Volume Name for the
  146.     move.l    d0,a0            ; ...requested drive.
  147.     moveq    #0,d0
  148.     move.b    (a0)+,d0        ; get length of Volume name
  149.     move.l    a4,d1            ; and find
  150.     add.l    #v_STRING,d1        ; ...where to store it
  151.     move.l    d1,a1
  152.     move.l    d1,v_STRPTR(a4)     ; ...(also, save for RawDoFmt)
  153. dlp:
  154.     move.b    (a0)+,(a1)+        ; copy name to string storage
  155.     dbra    d0,dlp
  156.     move.b    #0,(a1)         ; ...and remember to NULL term!
  157. df2:
  158.     lea    FMT(pc),a0        ; ...the format string
  159.     lea    v_STRPTR(a4),a1        ; ...the first print parm
  160.     lea    store(pc),a2        ; ...the storage routine address
  161.     lea    v_OUTPUT(a4),a3        ; ...the storage area
  162.     movem.l a3/a6,-(sp)        ; save a couple registers
  163.     move.l    _AbsExecBase,a6
  164.     Call    RawDoFmt        ; and figure it out!
  165.     movem.l (sp)+,a3/a6
  166.     move.l    d5,d1
  167.     move.l    a3,d2
  168.     moveq    #127,d0
  169.     moveq    #0,d3
  170. lenloop:
  171.     tst.b    (a3)+            ; look for null term
  172.     beq.s    printit
  173.     addq.l    #1,d3
  174.     dbra    d0,lenloop
  175. printit:
  176.     Call    Write            ; then print when found
  177.     bra.s    cleanup         ; and quit.
  178.  
  179. nomem:
  180.     move.l    d5,d1
  181.     move.l    #S_NOMEM,d2
  182.     move.l    #S_NOMSIZ,d3
  183.     Call    Write
  184.     bra.s    afterfree
  185.  
  186. noinfo:
  187.     move.l    d5,d1
  188.     move.l    #S_NOINFO,d2
  189.     move.l    #S_NINFSIZ,d3
  190.     Call    Write
  191.     bra.s    cleanup
  192.  
  193. nolock:
  194.     move.l    d5,d1
  195.     move.l    #S_NLOCK,d2
  196.     move.l    #S_NLKSIZ,d3
  197.     Call    Write
  198.     bra.s    FINISHED
  199.  
  200. cleanup:
  201.     ; start off by freeing allocated memory.
  202.     move.l    a5,a1
  203.     MOVEM.L D1-D7/A0-A6,-(SP)    ; in case FreeMem trashes
  204.     move.l    #id_SIZEOF,D0
  205.     move.l    _AbsExecBase,a6
  206.     Call    FreeMem
  207.     MOVEM.L (SP)+,D1-D7/A0-A6    ; restore registers
  208.  
  209. afterfree:
  210.  
  211.     ; May have to throw away two locks and do a cd here
  212.     move.l    d4,d1
  213.     cmpi.l    #FALSE,d6
  214.     beq.s    nocd
  215.  
  216.     Call    CurrentDir        ; get back to where we were
  217.     move.l    d0,d1
  218. nocd:
  219.     Call    UnLock
  220.     moveq    #RETURN_OK,D0
  221. FINISHED:
  222.     unlk    a4
  223.     rts
  224.  
  225. * Subroutines
  226. * ------------------------------------------------------------------------
  227.  
  228. ********************************************************************************
  229. * argtrim:    a routine to trim the end of a command line and null terminate
  230. *        it. This is achieved in the following manner:
  231. *        Start at the end and run back until a non-whitespace (nl/blank)
  232. *        character is encountered. If this is a quote, toss it.
  233. *        If the *first* character is a quote, bump the start address by
  234. *        one. No pretense of quote matching here.
  235. *         Inputs:    address in A0, length in D0
  236. *         Outputs:    address in A0.
  237. ********************************************************************************
  238.  
  239. argtrim:
  240.     movem.l d1-d7/a1-a6,-(sp)    ; save registers
  241.  
  242.     cmpi.b    #'"',(a0)               ; starts with a quote?
  243.     bne.s    checkline        ; no
  244.     subq    #1,d0            ; yes - decrement count
  245.     addq    #1,a0            ; and bump start address
  246.  
  247. checkline:
  248.     cmpi.b    #1,D0            ; length includes the newline
  249.     bne.s    isline            ; yes - there is something there
  250.     move.b    #0,(a0)         ; create null string
  251.     bra.s    argfini         ; done
  252.  
  253. isline:
  254. ;    strip off any run of blanks on the end...
  255.     move.l    a0,a1            ; computing end address of line
  256.     add.l    d0,a1            ;
  257.     subq    #2,a1            ;
  258.  
  259. toploop:
  260.     cmp.l    a0,a1
  261.     beq.s    empty            ; single char or run of blanks
  262.  
  263.     cmpi.b    #' ',(a1)
  264.     bne.s    endloop         ; finished the scan
  265.     subq    #1,a1            ; else back one more
  266.     bra.s    toploop         ; and try again
  267.  
  268. endloop:
  269.     cmpi.b    #'"',(a1)
  270.     beq.s    nullit
  271. nullnext:
  272.     addq    #1,a1
  273. nullit:
  274.     move.b    #0,(a1)
  275.     bra.s    argfini
  276. empty:                    ; could be blanks or a single char
  277.     cmpi.b    #' ',(a1)
  278.     bne.s    endloop         ; or maybe a single quote!
  279.     move.b    #0,(a0)
  280.  
  281. argfini:
  282.     movem.l (sp)+,d1-d7/a1-a6    ; restore registers
  283.     rts
  284.  
  285. ********************************************************************************
  286. * STORE:    routine used by _LVORawDoFmt to store output string
  287. ********************************************************************************
  288.  
  289. store:
  290.     move.b    d0,(a3)+
  291.     rts
  292.  
  293. ********************************************************************************
  294. * Data declarations
  295. ********************************************************************************
  296.  
  297.     CNOP    0,4
  298. DOSName     DOSNAME
  299. S_NLOCK     DC.B    'directory or file name not found',10
  300. S_NLKSIZ    equ    *-S_NLOCK
  301. S_NOINFO    DC.B    'Couldn''t get info for disk',10
  302. S_NINFSIZ    equ    *-S_NOINFO
  303. S_NOMEM     DC.B    'No memory',10
  304. S_NOMSIZ    equ    *-S_NOMEM
  305. FMT        DC.B    'Volume "%s:" has %ld bytes free.',10,0
  306.     END
  307.